<%@ page import="com.example.cdpbookstore.service.BookService" %>
<%@ page import="com.example.cdpbookstore.service.impl.BookServiceImpl" %>
<%@ page import="com.example.cdpbookstore.pojo.entity.Book" %>
<%@ page import="java.util.List" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    BookService bookService = new BookServiceImpl();

    String categoryId = request.getParameter("category");
    String search = request.getParameter("search");

    request.setAttribute("category", categoryId);
    request.setAttribute("search", search);

    List<Book> list;
    if (search != null) {
        list = bookService.search(search);
    } else {
        if (categoryId != null) {
            list = bookService.list(Long.valueOf(categoryId));
        } else {
            list = bookService.list();
        }
    }

    pageContext.setAttribute("list", list);
%>
<!DOCTYPE html>
<html lang="zh-Hans-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>布克书店 - 首页</title>

    <link rel="icon" type="image/png" href="image/web-icon.png">
    <link rel="stylesheet" type="text/css" href="lib/swiper-11.0.3/swiper-bundle.min.css">
    <link rel="stylesheet" type="text/css" href="lib/font-awesome-6.5.2/css/all.css">
    <link rel="stylesheet" type="text/css" href="css/common/com-a.css">
    <link rel="stylesheet" type="text/css" href="css/common/com-b.css">
    <link rel="stylesheet" type="text/css" href="css/index.css">
</head>

<body>
<div class="main">
    <jsp:include page="common/header1.jsp"/>

    <c:if test="${category == null && search == null}">
        <!-- banner 部分 -->
        <div class="banner">
            <div class="bn-center">
                <!-- 左：幻灯片 -->
                <div class="swiper">
                    <div class="swiper-wrapper">
                        <div class="swiper-slide"><img src="img/banner/1.jpeg" style="width: 800px; height: 300px;"
                                                       alt=""></div>
                        <div class="swiper-slide"><img src="img/banner/2.jpeg" style="width: 800px; height: 300px;"
                                                       alt=""></div>
                        <div class="swiper-slide"><img src="img/banner/3.jpg" style="width: 800px; height: 300px;"
                                                       alt=""></div>
                    </div>
                    <!-- 如果需要分页器 -->
                    <div class="swiper-pagination"></div>

                    <!-- 如果需要导航按钮 -->
                    <div class="swiper-button-prev"></div>
                    <div class="swiper-button-next"></div>

                    <!-- 如果需要滚动条 -->
                    <div class="swiper-scrollbar"></div>
                </div>

                <!-- 右：头像 -->
                <div class="userInfo">
                        <%--                <jsp:useBean id="customer" scope="request" type="com.example.cdpbookstore.pojo.entity.Customer"/>--%>
                    <c:if test="${customer == null || customer.avatar == null}">
                        <c:if test="${customer == null}">
                            <%-- 没登录，点击头像可以登录 --%>
                            <a href="login.jsp"><img class="avatar" src="image/avatar.png" alt=""></a>
                        </c:if>
                        <c:if test="${customer != null}">
                            <%-- 已登录 --%>
                            <img class="avatar" src="image/avatar.png" alt="">
                        </c:if>
                    </c:if>
                    <c:if test="${!(customer == null || customer.avatar == null)}">
                        <img class="avatar" src="/img/avatar/${customer.avatar}" alt="">
                    </c:if>
                    <p class="hello">Hi, ${customer.username}! 你好</p>
                    <div class="itemWrap">
                        <a href="shopping-cart.jsp">
                            <div class="item">
                                <!-- <img src="image/icon/shopping-cart.png" alt=""> -->
                                <i class="fa-solid fa-cart-shopping"></i>
                                <p>购物车</p>
                            </div>
                        </a>

                        <a href="order.jsp">
                            <div class="item">
                                <!-- <img src="image/icon/order.png" alt=""> -->
                                <i class="fa-solid fa-clipboard"></i>
                                <p>我的订单</p>
                            </div>
                        </a>

                        <a href="user.jsp">
                            <div class="item">
                                <!-- <img src="image/icon/user.png" alt=""> -->
                                <i class="fa-solid fa-user"></i>
                                <p>我的账户</p>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </c:if>

    <!-- 商品列表部分 -->
    <div class="bookTable">
        <div class="bt-center">
            <c:forEach var="book" items="${list}">
                <div class="bt-card">
                    <div class="cover">
                        <a href="book.jsp?book=${book.id}">
                            <img src="/img/book/${book.images}" alt="">
                        </a>
                    </div>
                    <div class="box">
                        <div class="price">
                            <div class="current-price">￥${book.currentPrice}</div>
                            <div class="discount">(${book.discount}折)</div>
                            <div class="original-price">￥${book.originalPrice}</div>
                        </div>
                        <div class="bk-name">
                            <a href="book.jsp?book=${book.id}">${book.bookName}</a>
                        </div>
                        <div class="bk-info">
                            <span>${book.author}</span><span>&nbsp;/&nbsp;</span><span>${book.press}</span>
                        </div>
                        <div class="bk-btn">
                            <a href="add-to-cart?id=${book.id}" class="btn btn-add">加入购物车</a>
                            <a href="order-now?id=${book.id}&quantity=1" class="btn btn-buy">立即购买</a>
                        </div>
                    </div>
                </div>
            </c:forEach>
        </div>
    </div>

    <jsp:include page="common/footer1.jsp"/>
</div>
<script src="lib/swiper-11.0.3/swiper-bundle.min.js"></script>
<script src="js/index.js"></script>

</body>
</html>